package com.net.oil.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.net.oil.R;
import com.net.oil.action.ActionDetailActivity;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.http.AppUrl;
import com.net.oil.main.bean.AdBean;
import com.net.oil.main.contract.WelcomeContract;
import com.net.oil.main.presenter.WelcomePresenter;
import com.net.oil.util.SharedPreferencesKey;
import com.net.oil.util.SharedPreferencesUtil;
import com.net.oil.util.glide.GlideUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeActivity extends BaseActivity<WelcomePresenter> implements WelcomeContract.View {

    @BindView(R.id.welcome_image)
    ImageView welcomeImage;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.time_layout)
    LinearLayout timeLayout;


    CountDownTimer countDownTimer = new CountDownTimer(5 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if (!isFinishing()) {
                time.setText(String.valueOf(millisUntilFinished / 1000));
            } else {
                cancel();
            }
        }

        @Override
        public void onFinish() {
            if (!isFinishing()) {
                openActivity();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        ButterKnife.bind(this);
        presenter = new WelcomePresenter(this);
        initData();

        initView();
    }

    private void initView() {
        boolean isFirstInstall = (boolean) SharedPreferencesUtil.getBoolenParm(this, SharedPreferencesKey.IS_FIRST_INSTALL, true);
        if (isFirstInstall) {
            startActivity(new Intent(this, IntroduceActivity.class));
            finish();
        } else {
            addAdvertisingView();
        }

        timeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity();
            }
        });


    }

    private void addAdvertisingView() {
        List<AdBean> adBeans;
        String result = SharedPreferencesUtil.getStringParam(this, SharedPreferencesKey.WELCOME_ADVERTISING, "");
        if (!TextUtils.isEmpty(result)) {
            adBeans = new Gson().fromJson(result, new TypeToken<List<AdBean>>() {
            }.getType());

            if (adBeans != null) {
                //要显示广告，默认从0开始
                int position = SharedPreferencesUtil.getIntParm(this, SharedPreferencesKey.ADVERTISING_POSITION, 0);
                if (position >= adBeans.size()) {
                    position = 0;
                }

                final AdBean adBean = adBeans.get(position);
                time.setText(String.valueOf(5));
                GlideUtil.getInstance().loadImage(this, welcomeImage, AppUrl.getImageUrl(adBean.getImage()), R.drawable.start_page);
                SharedPreferencesUtil.setIntParm(this, SharedPreferencesKey.ADVERTISING_POSITION, position + 1);        //保存下次要显示的广告
                welcomeImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (adBean.getType() == 1){     //type = 1 跳转到活动
                            Intent intentMain = new Intent(WelcomeActivity.this, MainActivity.class);
                            Intent intentAction = new Intent(WelcomeActivity.this, ActionDetailActivity.class);
                            intentAction.putExtra(Constant.INTENT_ACTION_KEY, adBean.getActionId());
                            Intent[] intents  = new Intent[2];
                            intents[0] = intentMain;
                            intents[1] = intentAction;
                            startActivities(intents);
                            finish();
                        }
                    }
                });
            }
        }
        countDownTimer.start();
    }

    private void openActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    private void initData() {
        presenter.getAdList();
    }

    /**
     * 获取启动页图片成功
     *
     * @param adBeans 启动页图片
     */
    @Override
    public void getAdListSuccess(List<AdBean> adBeans) {
        if (adBeans != null && adBeans.size() > 0) {
            SharedPreferencesUtil.setStringParam(this, SharedPreferencesKey.WELCOME_ADVERTISING,
                    new Gson().toJson(adBeans));
        }
    }

    /**
     * 获取启动页图片失败
     */
    @Override
    public void getAdListFailed() {

    }
}
